/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calafie.builder.ihm.generated;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.DefaultCaret;

import calafie.builder.Util;
import calafie.builder.ihm.controleur.fiche.CheckActionListener;
import calafie.builder.ihm.modele.swing.ModeleOrdreOngletOrdre;
import calafie.builder.ihm.modele.type.TypeLegalite;
import calafie.builder.ihm.modele.type.TypeOrdre;
import calafie.builder.jaxb.Ordre;

/**
 * 
 * @author Dart
 */
public class PaneOrdre extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = -5667852833993737108L;
    private JTextArea descriptionLabel;
    private JLabel nomOrdreLabel;
    private JLabel potLabel;
    private JLabel coutLabel;
    private ModeleOrdreOngletOrdre modeleOrdre;
    private PopUpOrdre popOrdre;
    private javax.swing.JButton copyOrdre;
    private JScrollPane jScrollPane2;
    /**
     * Creates new form PanelOrdreG
     */
    public PaneOrdre() {
        initComponents();

        popOrdre = new PopUpOrdre();
        

        typeFiltreCombo.setModel(new DefaultComboBoxModel(TypeOrdre
                .getModele()));

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    private void initComponents() {

        paneButtonOrdre = new JPanel();
        newButton = new JButton();
        editButton = new JButton();
        deleteButton = new JButton();
        JScrollPane tableListe = new JScrollPane();
        tableOrdres = new JTable();
        paneOrdre = new JPanel();
        jScrollPane2 = new JScrollPane();
        descriptionLabel = new JTextArea();
        nomOrdreLabel = new JLabel();
        coutLabel = new JLabel();
        potLabel = new JLabel();
        typeFiltreCombo = new JComboBox();
        copyOrdre = new JButton();
        paneButtonOrdre.setBorder(BorderFactory
                .createEtchedBorder());
        modeleOrdre = new ModeleOrdreOngletOrdre();
        tableOrdres.setModel(modeleOrdre);
        descriptionLabel.setEditable(false);
        
        newButton.setText(Util.getMessage("builder.button.new"));
        editButton.setText(Util.getMessage("builder.button.edit"));
        deleteButton.setText(Util.getMessage("builder.button.delete"));
        copyOrdre.setText(Util.getMessage("builder.button.copy"));
        
        descriptionLabel.setLineWrap(true);        
        descriptionLabel.setWrapStyleWord(true);
       
        DefaultCaret caret = (DefaultCaret) descriptionLabel.getCaret();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        
        editButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ouvrirPopUp(true);
            }
        });

        newButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ouvrirPopUp(false);
            }
        });

        deleteButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                supprimer();
            }
        });
        tableOrdres.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        
        
        tableOrdres.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            
            public void valueChanged(ListSelectionEvent e) {
               selection();                
            }
        });
        
        
        copyOrdre.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                copier();
                
            }


        });
        
        tableOrdres.setAutoCreateRowSorter(true);

        org.jdesktop.layout.GroupLayout paneButtonOrdreLayout = new org.jdesktop.layout.GroupLayout(
                paneButtonOrdre);
        paneButtonOrdre.setLayout(paneButtonOrdreLayout);
        paneButtonOrdreLayout.setHorizontalGroup(paneButtonOrdreLayout
                .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(paneButtonOrdreLayout
                        .createSequentialGroup()
                        .addContainerGap()
                        .add(newButton)
                        .addPreferredGap(
                                org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(copyOrdre)
                        .addPreferredGap(
                                org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(editButton)
                        .addPreferredGap(
                                org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(deleteButton)
                        .addContainerGap(
                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)));
        paneButtonOrdreLayout.setVerticalGroup(paneButtonOrdreLayout
                .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(paneButtonOrdreLayout
                        .createParallelGroup(
                                org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(newButton).add(copyOrdre).add(editButton).add(deleteButton)));


        
        
       
        tableListe.setBorder(BorderFactory.createEtchedBorder());

        tableListe.setViewportView(tableOrdres);

        paneOrdre.setBorder(BorderFactory.createEtchedBorder());
        jScrollPane2.setViewportView(descriptionLabel);

        JPanel panCheck = new JPanel();
        
        panCheck.add(CheckActionListener.addActionListener(new JCheckBox(), TypeOrdre.BATIMENTS, modeleOrdre));
        panCheck.add(CheckActionListener.addActionListener(new JCheckBox(), TypeOrdre.PERSONNAGE, modeleOrdre));
        panCheck.add(CheckActionListener.addActionListener(new JCheckBox(), TypeOrdre.RENCONTRES, modeleOrdre));
        panCheck.add(CheckActionListener.addActionListener(new JCheckBox(), TypeOrdre.ORGANISATIONS, modeleOrdre));
        panCheck.add(CheckActionListener.addActionListener(new JCheckBox(), TypeOrdre.PREROGATIVES, modeleOrdre));
        
        
        
        org.jdesktop.layout.GroupLayout paneOrdreLayout = new org.jdesktop.layout.GroupLayout(
                paneOrdre);
        paneOrdre.setLayout(paneOrdreLayout);
        paneOrdreLayout
                .setHorizontalGroup(paneOrdreLayout
                        .createParallelGroup(
                                org.jdesktop.layout.GroupLayout.LEADING)
                        .add(paneOrdreLayout
                                .createSequentialGroup()
                                .addContainerGap()
                                .add(paneOrdreLayout
                                        .createParallelGroup(
                                                org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jScrollPane2,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                425, Short.MAX_VALUE)
                                        .add(nomOrdreLabel,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)
                                        .add(coutLabel,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)
                                        .add(potLabel,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE))
                                .addContainerGap()));
        paneOrdreLayout.setVerticalGroup(paneOrdreLayout.createParallelGroup(
                org.jdesktop.layout.GroupLayout.LEADING).add(
                paneOrdreLayout
                        .createSequentialGroup()
                        .add(10, 10, 10)
                        .add(nomOrdreLabel,15,15,15)
                        .addPreferredGap(
                                org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jScrollPane2,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                93,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(
                                org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(coutLabel,15,15,15 )
                        .add(potLabel,15,15,15)
                        .addContainerGap()));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
                this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout
                        .createSequentialGroup()
                        .add(layout
                                .createParallelGroup(
                                        org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout
                                        .createSequentialGroup()
                                        
                                        .add(layout
                                                .createParallelGroup(
                                                        org.jdesktop.layout.GroupLayout.LEADING)
                                                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                                                        tableListe)
                                                .add(org.jdesktop.layout.GroupLayout.TRAILING,
                                                        layout.createSequentialGroup()
                                                                .add(0,
                                                                        0,
                                                                        Short.MAX_VALUE)
                                                                )))
                                .add(layout
                                        .createSequentialGroup()
                                        .add(layout
                                                .createParallelGroup(
                                                        org.jdesktop.layout.GroupLayout.LEADING)
                                                .add(layout
                                                        .createSequentialGroup()
                                                        .add(140, 140, 140)
                                                        .add(layout
                                                                .createParallelGroup(
                                                                        org.jdesktop.layout.GroupLayout.TRAILING)
                                                                .add(paneOrdre,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                                        
                                                                .add(paneButtonOrdre,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                                        org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                                                        .add(panCheck)
                                                .add(layout
                                                        .createSequentialGroup()
                                                        .addContainerGap()
                                                       ))
                                       ))
                        .addContainerGap()));
        layout.setVerticalGroup(layout
                .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout
                        .createSequentialGroup()
                        
                        .add(paneOrdre,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(
                                org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(paneButtonOrdre,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(panCheck)
                        .add(tableListe,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                378,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                             .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    private JButton deleteButton;
    private JButton editButton;
    private JButton newButton;
    private JPanel paneButtonOrdre;
    private JPanel paneOrdre;
    private JTable tableOrdres;
    private JComboBox typeFiltreCombo;

    
    private void clearOrdre(){
        descriptionLabel.setText("");
        nomOrdreLabel.setText("");
        coutLabel.setText("");
        potLabel.setText("");
    }
    
    public void setOrdre(Ordre ordre) {
        
        String titre = "";

        titre += ordre.getNom();
        if (!ordre.isLegal()) {
            String gravite = TypeLegalite.valueOf(ordre.getGravite()).getNom();
            titre += " (" + gravite + (ordre.isPolitique() ? " politique" : "")
                    + ")";
        }
        
        String pot = "";
        String cout = "Coût : ";
        cout += ordre.getArgent() + "fk |";
        cout += " Fatigue: " + ordre.getPv() + " PdV ";

        if (ordre.isAutomatique()) {
            cout += " | Automatique";
        } else {
            cout += " | Difficulté: " + ordre.getDifficulte();
            pot = "Potentiel : " + ordre.getCaracteristique();
            if (ordre.getOposition().length() > 0) {
                pot += "-" + ordre.getOposition();
            }
            pot += "+" + ordre.getCompetence();
        }

        DefaultCaret caret = (DefaultCaret) descriptionLabel.getCaret();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        descriptionLabel.setText(ordre.getDescription());
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        
        jScrollPane2.getViewport().setViewPosition(new Point(0, 0));
        nomOrdreLabel.setText(titre);
        coutLabel.setText(cout);
        potLabel.setText(pot);
    }

    private void ouvrirPopUp(boolean modif) {

        clearOrdre();

        
        if (modif) {
            int index = tableOrdres.getSelectedRow();
            if (index == -1){
                return;
             }
            Ordre ordre = popOrdre.ouverture(modeleOrdre.getItem(index));
            if (ordre != null) {
                
                modeleOrdre.modificationOrdre(ordre);
                setOrdre(ordre);
            } else {
                tableOrdres.getSelectionModel().clearSelection();
            }
        } else {
            Ordre ordre = popOrdre.ouverture(null);
            if (ordre != null) {
                modeleOrdre.addOrdre(ordre);
            }

        }
    }

    private void supprimer(){
        clearOrdre();
        int index = tableOrdres.getSelectedRow();
        modeleOrdre.removeItem(index);
    }
    

    private void selection() {
        int index = tableOrdres.getSelectedRow();
        if (index == -1){
           return;
        }
        index = tableOrdres.getRowSorter().convertRowIndexToModel(index);
        
        Ordre ordre = modeleOrdre.getItem(index);
        setOrdre(ordre);

    }
    
    private void copier() {
        
        int index = tableOrdres.getSelectedRow();
        if (index == -1){
           return;
        }
        Ordre ordre = modeleOrdre.getItem(index);
        modeleOrdre.addOrdre(Util.cloneOrdre(ordre));
       
    }
    
}
